import {
	OPT_GET,
	TAB_DISEASES,
	TAB_DISEASEORDERLOG,
	TAB_DISEASEGRADE,
	TAB_DISEASECONTENT,
	TAB_LASTFILLINGRECORD,
	TAB_DISEASESTATUS,
	TAB_KEYWORDS,
	TAB_REPAIR,
	TAB_IMAGES,
	TAB_STATION
} from './common.js'
//巡检的所有接口
import * as XUNJIAN from '@/api/engineeringMaintenance/engineeringMaintenance.js'
import {
	getPicture
} from '@/api/doc.js' //获取图片
import {
	localPathToBase64
} from "@/utils/imageOperation.js"

/** 离线缓存的选项数据对应的离线文件路径（或文件名） */
const _map = new Map();

_map.set(XUNJIAN.diseaseOrderGetDetails, {
	type: OPT_GET,
	order: 1,
	willSubmit: false,
	single: true,
	where: param => `id=${param.id} order by id desc `,
	tableName: TAB_DISEASES,
	children: [{
		fieldName: "diseaseOrderLogDto",
		single: true,
		tableName: TAB_DISEASEORDERLOG,
		fnCondition: parent => `diseaseOrderId=${parent.id}`
	}],
	resultMapping: (data) => {
		return {
			...data,
			isInfluenceEquipment: !!data.isInfluenceEquipment,
			isProtectionSection: !!data.isProtectionSection,
			isSandySoil: !!data.isSandySoil,
			_orderType: data.orderType
		}
	}
})

//获取列表数据
_map.set(XUNJIAN.diseaseOrderGetList, {
	type: OPT_GET,
	dataFieldName: 'list',
	tableName: TAB_DISEASES,
	where: param => `
		lineId='${param.params.lineId}'
		and lineSiteSectionId='${param.params.lineSiteSectionId}'
		and lineSiteSectionType='${param.params.lineSiteSectionType}'
	`,
	children: [{
		fieldName: "fileListImg",
		single: false, //是否为单个对象 
		tableName: TAB_IMAGES,
		fnCondition: parent => parent.docId ? `docInfoId=${parent.docId}` : '1=0'
	}],
	resultMapping: (data) => {
		// console.warn(data)
		return data
	}

})

//获取所有详情列表
_map.set(XUNJIAN.getDetailsList, {
	type: OPT_GET,
	where: ({
		lineId,
		lineSiteSectionId,
		lineSiteSectionType
	}) => `1=1
		and lineId='${lineId}'
		and lineSiteSectionId='${lineSiteSectionId}'
		and lineSiteSectionType='${lineSiteSectionType}'
	`,
	tableName: TAB_DISEASEORDERLOG
	// children: [
	// 	{fieldName:"diseaseOrderLogDto", tableName: TAB_DISEASEORDERLOG, fnCondition: parent => `_id=${parent._id}` },
	// ],
})

//获取上次填报记录接口
_map.set(XUNJIAN.getLastFillingRecord, {
	type: OPT_GET,
	single: true,
	where: param => "1=1",
	tableName: TAB_LASTFILLINGRECORD,
})

//查询-病害等级(一级选项)
_map.set(XUNJIAN.diseaseGradeList, {
	type: OPT_GET,
	dataFieldName: 'list',
	where: param => "1=1",
	tableName: TAB_DISEASEGRADE
})

//查询-病害内容(二级)
_map.set(XUNJIAN.getDiseaseContent, {
	type: OPT_GET,
	tableName: TAB_DISEASECONTENT,
	where: ({
		diseaseGradeId,
		code
	}) => `diseaseGradeId=${diseaseGradeId} and code = '${code}'`,
})

//查询-病害关键字
_map.set(XUNJIAN.diseaseKeywordsList, {
	type: OPT_GET,
	where: param => "1=1",
	dataFieldName: 'list',
	tableName: TAB_KEYWORDS,
})
//查询-病害现状
_map.set(XUNJIAN.diseaseStatusList, {
	type: OPT_GET,
	where: param => `diseaseStatus like '%${param.params.keyword}%'`,
	dataFieldName: 'list',
	tableName: TAB_DISEASESTATUS,
})
//查询-修复措施
_map.set(XUNJIAN.diseaseMaterialList, {
	type: OPT_GET,
	where: param => `(code like '%${param.params.queryStr}%' or makt like '%${param.params.queryStr}%')`,
	dataFieldName: 'list',
	tableName: TAB_REPAIR,
})

//查询-获取图片信息
_map.set(getPicture, {
	type: OPT_GET,
	where: param => param.docId ? `docInfoId=${param.docId}` : '1=0',
	tableName: TAB_IMAGES,
	resultMapping: async (data) => {
		console.log(data, '---135---');
		return {
			...data
		}
	}
})

//查询-获取区间站点信息
_map.set(XUNJIAN.getPlanLineSectionSiteStateTreeByLike, {
	type: OPT_GET,
	where: param => '1=1',
	tableName: TAB_STATION,
	combin: pm => {
		console.log(pm, 'pm148');
		const lines = []
		const pmList = []
		pm.forEach(s => {
			const pm = pmList.find(p => p.id === s.pmId);
			if (!pm) {
				pmList.push({
					id: s.pmId,
					name: s.pmName,
					list: [{
						id: s.lineId,
						name: s.lineName,
						branchLineName:s.branchLineName,
						list: [{
							id: s.categoryId,
							name: s.categoryName,
							list: [{
								id: s.id,
								name: s.name,
								amount: s.amount,
								mileage: s.mileage,
								type: s.type
							}]
						}]
					}]
				});
			} else {
				const line = pm.list.find(l => l.id === s.lineId);
				if (!line) {
					pm.list.push({
						id: s.lineId,
						name: s.lineName,
						branchLineName:s.branchLineName,
						list: [{
							id: s.categoryId,
							name: s.categoryName,
							list: [{
								id: s.id,
								name: s.name,
								amount: s.amount,
								mileage: s.mileage,
								type: s.type
							}]
						}]
					});
				} else {
					const cat = line.list.find(c => c.id === s.categoryId);
					if (!cat) {
						line.list.push({
							id: s.categoryId,
							name: s.categoryName,
							list: [{
								id: s.id,
								name: s.name,
								amount: s.amount,
								mileage: s.mileage,
								type: s.type
							}]
						});
					} else {
						cat.list.push({
							id: s.id,
							name: s.name,
							amount: s.amount,
							mileage: s.mileage,
							type: s.type
						});
					}
				}
			}
		});

		// stations.forEach(s=>{
		// 	const line = lines.find(l=>l.id===s.lineId)
		// 	if(!line) {
		// 		lines.push({
		// 			id: s.lineId,
		// 			name: s.lineName,
		// 			list: [{
		// 				id: s.categoryId,
		// 				name: s.categoryName,
		// 				list: [{id: s.id, name: s.name, amount: s.amount, mileage: s.mileage, type: s.type}]
		// 			}]					
		// 		})
		// 	}else{
		// 		const cat = line.list.find(c=>c.id===s.categoryId)
		// 		if(!cat) {
		// 			line.list.push({
		// 				id: s.categoryId,
		// 				name: s.categoryName,
		// 				list: [{id: s.id, name: s.name, amount: s.amount, mileage: s.mileage, type: s.type}]
		// 			})
		// 		}else{
		// 			cat.list.push({
		// 				id: s.id, name: s.name, amount: s.amount, mileage: s.mileage, type: s.type,
		// 			})
		// 		}
		// 	}
		// })
		console.warn('---------160行', pmList)
		return pmList
	}
})
export const apiDataMapping = _map;